package com.xzzz.backend.bsms.server.rbac.user;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xzzz.common.base.util.BeanUtil;
import com.xzzz.common.base.util.DateUtils;
import com.xzzz.common.base.util.PrimaryKeyUtil;
import com.xzzz.backend.bsms.server.rbac.user.pojo.UserLoginRecordEntity;
import com.xzzz.irda.auth.server.authentication.AuthLoginEvent;
import com.xzzz.irda.auth.server.pojo.LoginEventMetadata;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class UserLoginRecordService extends ServiceImpl<UserLoginRecordMapper, UserLoginRecordEntity> {

    /**
     * 记录文章修改日志
     *
     * @param event 事件
     */
    @Async
    @EventListener(AuthLoginEvent.class)
    public void save(AuthLoginEvent event) {
        LoginEventMetadata login = (LoginEventMetadata) event.getSource();

        UserLoginRecordEntity record = BeanUtil.toObj(login, UserLoginRecordEntity.class);
        record.setId(PrimaryKeyUtil.nextId());
        record.setLoginTime(DateUtils.date());
        record.setUserAgent(login.getUserAgent());
        record.setIp(login.getLoginIp());

        baseMapper.insert(record);
    }

}
